<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/1/18
 * Time: 17:04
 */

namespace app\admin\controller;


use app\common\controller\Backend;
use think\Db;
use think\Exception;

/**
 * 寺庙管理
 *
 * @icon fa fa-users
 * @remark
 */
class Temple extends Backend
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Temple');
    }

    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->where(array('is_delete'=>0))
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->where(array('is_delete'=>0))
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $key=>&$value){
                $note_ids_list = model('Note')
                    ->where(array('temple_id'=>$value['id']))
                    ->field('id')
                    ->select();
                $note_ids = [];
                foreach ($note_ids_list as $k=>$v){
                    $note_ids[]=$v['id'];
                }
                $solve_count = model('UserNote')
                    ->where(array('is_solve'=>1))
                    ->where('note_id','in',$note_ids)
                    ->count();
                $qr_count = model('UserNote')
                    ->where('note_id','in',$note_ids)
                    ->count();
                $value['solve_count'] = $solve_count;
                $value['qr_count'] = $qr_count;
            }
            unset($value);
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 二维码
     * @return string
     */
    public function qr_code($ids=''){
        $host = 'http://'.$_SERVER['HTTP_HOST'];
        $this->assignconfig('host',$host);
        $this->assignconfig('temple_id',$ids);
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                $this->model->create($params);
                $this->success();
            }
            $this->error();
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row)
            $this->error(__('No Results were found'));
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                $row->save($params);
                $this->success();
            }
            $this->error();
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 用户解签列表
     * @param string $ids
     * @return string
     */
    public function user_note_list($ids=''){
        if ($this->request->isAjax()) {
            $this->model = model('UserNote');
            $this->relationSearch = true;
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $note_ids_list = model('Note')
                ->where(array('temple_id' => $ids))
                ->field('id')
                ->select();
            $note_ids = [];
            foreach ($note_ids_list as $k => $v) {
                $note_ids[] = $v['id'];
            }
            $total = model('UserNote')
                ->with('user,note')
                ->where($where)
                ->where('note_id','in',$note_ids)
                ->order($sort, $order)
                ->count();

            $list = model('UserNote')
                ->with('user,note')
                ->where($where)
                ->where('note_id','in',$note_ids)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);
            foreach ($list as $key=>&$value){
                $value['temple_name'] = model('Temple')->where(array('id'=>$value['note']['temple_id']))->value('name');
                $solve_note_ids = $value['note']['solve_note_ids'];
                $solve_note_list = model('SolveNote')->where('id','in',$solve_note_ids)->select();
                $solve_note_name_list =[];
                foreach ($solve_note_list as $k=>$v){
                    $solve_note_name_list[]=$v['name'];
                }
                $content_list = model('Content')
                    ->where(array('note_id'=>$value['note']['id']))
                    ->select();
                $content_array = [];
                foreach ($content_list as $k=>$content){
                    $content_array[]=$content['content'];
                }
                $value['solve_note_text'] = implode(',', $solve_note_name_list);
                $value['content_text'] = implode(',', $content_array);
            }
            return json($result);
        }
        $this->assignconfig('temple_id',$ids);
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids)
        {
            Db::startTrans();
            try {
                $result = Db::name('Temple')->where(array('id'=>$ids))->update(array('is_delete'=>1));
                if($result){
                    Db::commit();
                    $this->success('删除成功!');
                }else {
                    $this->error('删除失败!');
                }
            }catch (Exception $e){
                Db::rollback();
                $this->error('删除失败!');
            }

        }
        $this->error();
    }

    /**
     * 批量更新
     * @internal
     */
    public function multi($ids = "")
    {
        // 管理员禁止批量操作
        $this->error();
    }
}